package com.openbravo.pos.panels;

import java.awt.Toolkit;
import com.openbravo.bean.Product;
import com.openbravo.bean.ref.I_RefProduct;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.pos.base.AppLocal;

import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import cn.ekuma.epos.core.util.ProductSwingUtil;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.BorderLayout;
import java.awt.Dimension;

public abstract class AbstractRefProductEditor<T extends I_RefProduct> extends
		AbstractChildDAOJEditor<T, String> {

	private final I_DataLogicSales m_dlSales;
	boolean showUnit;

    /** Creates new form JProductBomEditor */
    public AbstractRefProductEditor(I_DataLogicSales app, DirtyManager dirty,boolean showUnit) {
        super(dirty);
        m_dlSales = app;
        this.showUnit=showUnit;
        initComponents();
        m_jProduct.getDocument().addDocumentListener(dirty);
        jLabel1.setVisible(showUnit);
        jUnit.setVisible(showUnit);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    protected void initComponents() {
        
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(10, 130));
        panel.setLayout(null);
        
                jLabel3 = new javax.swing.JLabel();
                jLabel3.setBounds(10, 13, 125, 15);
                panel.add(jLabel3);
                
                        jLabel3.setText(AppLocal.getIntString("label.prodref"));
                        m_jReference = new javax.swing.JTextField();
                        m_jReference.setBounds(139, 10, 241, 21);
                        panel.add(m_jReference);
                        jLabel4 = new javax.swing.JLabel();
                        jLabel4.setBounds(10, 44, 125, 15);
                        panel.add(jLabel4);
                        
                                jLabel4.setText(AppLocal.getIntString("label.prodbarcode"));
                                m_jBarcode = new javax.swing.JTextField();
                                m_jBarcode.setBounds(139, 41, 241, 21);
                                panel.add(m_jBarcode);
                                m_jEnter2 = new javax.swing.JButton();
                                m_jEnter2.setBounds(386, 41, 49, 25);
                                panel.add(m_jEnter2);
                                
                                        m_jEnter2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/apply.png"))); // NOI18N
                                        m_jEnter1 = new javax.swing.JButton();
                                        m_jEnter1.setBounds(386, 10, 49, 25);
                                        panel.add(m_jEnter1);
                                        
                                                m_jEnter1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/apply.png"))); // NOI18N
                                                m_jProduct = new javax.swing.JTextField();
                                                m_jProduct.setBounds(10, 72, 378, 21);
                                                panel.add(m_jProduct);
                                                
                                                        m_jProduct.setEditable(false);
                                                        m_jSearch = new javax.swing.JButton();
                                                        m_jSearch.setBounds(394, 72, 49, 25);
                                                        panel.add(m_jSearch);
                                                        
                                                                m_jSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
                                                                jLabel1 = new javax.swing.JLabel();
                                                                jLabel1.setBounds(10, 106, 120, 15);
                                                                panel.add(jLabel1);
                                                                
                                                                        jLabel1.setText(AppLocal.getIntString("label.includeUnits"));
                                                                        jUnit = new javax.swing.JTextField();
                                                                        jUnit.setBounds(140, 103, 240, 21);
                                                                        panel.add(jUnit);
                                                                m_jSearch.addActionListener(new java.awt.event.ActionListener() {
                                                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                                                                        m_jSearchActionPerformed(evt);
                                                                    }
                                                                });
                                                        m_jProduct.addActionListener(new java.awt.event.ActionListener() {
                                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                                                m_jProductActionPerformed(evt);
                                                            }
                                                        });
                                                m_jEnter1.addActionListener(new java.awt.event.ActionListener() {
                                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                                                        m_jEnter1ActionPerformed(evt);
                                                    }
                                                });
                                        m_jEnter2.addActionListener(new java.awt.event.ActionListener() {
                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                                m_jEnter2ActionPerformed(evt);
                                            }
                                        });
                                
                                        m_jBarcode.addActionListener(new java.awt.event.ActionListener() {
                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                                m_jBarcodeActionPerformed(evt);
                                            }
                                        });
                        
                                m_jReference.addActionListener(new java.awt.event.ActionListener() {
                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                                        m_jReferenceActionPerformed(evt);
                                    }
                                });
        setLayout(new BorderLayout(0, 0));
        add(panel, BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void m_jReferenceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jReferenceActionPerformed
        this.assignProductByReference();
}//GEN-LAST:event_m_jReferenceActionPerformed

    private void m_jEnter1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jEnter1ActionPerformed
        this.assignProductByReference();
}//GEN-LAST:event_m_jEnter1ActionPerformed

    private void m_jEnter2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jEnter2ActionPerformed
        this.assignProductByCode();
}//GEN-LAST:event_m_jEnter2ActionPerformed

    private void m_jBarcodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jBarcodeActionPerformed
        this.assignProductByCode();
}//GEN-LAST:event_m_jBarcodeActionPerformed

    private void m_jProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jProductActionPerformed

}//GEN-LAST:event_m_jProductActionPerformed

    private void m_jSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jSearchActionPerformed

        assignProduct(JProductFinder.showMessage(this, m_dlSales, JProductFinder.PRODUCT_NORMAL));
    }//GEN-LAST:event_m_jSearchActionPerformed


    protected  void assignProduct(Product prod) {
        if (m_jSearch.isEnabled()) {
            dirty.setDirty(true);
            obj.setProductID(prod.getID());
            obj.setIncludeProduct(prod);
            this.updateUI(true, state);
        }
    }

    private void assignProductByCode() {
            Product prod = ProductSwingUtil.getProductInfo(this, m_dlSales, m_jBarcode.getText());
            assignProduct(prod);
            if (prod == null) {
                Toolkit.getDefaultToolkit().beep();
            }
    }


    private void assignProductByReference() {
        try {
            Product prod = m_dlSales.getProductInfoByReference(m_jReference.getText());
            assignProduct(prod);
            if (prod == null) {
                Toolkit.getDefaultToolkit().beep();
            }
        } catch (BasicException eData) {
            assignProduct(null);
            MessageInf msg = new MessageInf(eData);
            msg.show(this);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    protected javax.swing.JTextField jUnit;
    private javax.swing.JTextField m_jBarcode;
    private javax.swing.JButton m_jEnter1;
    private javax.swing.JButton m_jEnter2;
    private javax.swing.JTextField m_jProduct;
    private javax.swing.JTextField m_jReference;
    private javax.swing.JButton m_jSearch;
    // End of variables declaration//GEN-END:variables


    @Override
    public void updateUI(boolean editAble, int state) {
        if (obj.getIncludeProduct() == null) {
                obj.setIncludeProduct(null);
                m_jReference.setText(null);
                m_jBarcode.setText(null);
                m_jProduct.setText(null);
            } else {
                m_jReference.setText(obj.getIncludeProduct().getReference());
                m_jBarcode.setText(obj.getIncludeProduct().getCode());
                m_jProduct.setText(obj.getIncludeProduct().getReference() + " - " + obj.getIncludeProduct().getName());
            }
        //jUnit.setText(Formats.DOUBLE.formatValue(obj.getUnits()));
    }

    public void refresh() {
    }
}
